package com.vq3.digitalizationsoftwareintegrates.Servlet.BusinessCenter.Spot_Check;


import com.vq3.digitalizationsoftwareintegrates.Entity.Info.ResultInfo;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.MongoDBEntity.MongoDB_S_C_WebSocketMessageEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.S_C_DatesEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.Permission_TBEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.UserEntity;
import com.vq3.digitalizationsoftwareintegrates.MongoDB.Spot_Check.MongoDB_S_C_Point_ContentDao;
import com.vq3.digitalizationsoftwareintegrates.Service.MessageCenter.MessageCenterService;
import com.vq3.digitalizationsoftwareintegrates.Service.Spot_Check.Spot_CheckExceptionService;
import com.vq3.digitalizationsoftwareintegrates.Utils.PackagedDataUtil.Impl.PackagedDataImpl;
import com.vq3.digitalizationsoftwareintegrates.Utils.PackagedDataUtil.PackagedData;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("Spot_Check_Exception")
public class Spot_CheckExceptionServlet {

    @Resource
    Spot_CheckExceptionService spot_checkExceptionService;



    /**承接工单
     *
     */
    @RequestMapping("acceptWorkOrders")
    public void acceptWorkOrders(@RequestParam String SCSailings, @RequestParam String date, @RequestParam String pointName, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
        PackagedData packagedData = new PackagedDataImpl();
        ResultInfo resultInfo = new ResultInfo();
        boolean b = false;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();

        UserEntity userEntity = (UserEntity) httpServletRequest.getSession().getAttribute("USER");
        for (GrantedAuthority authority : authorities) {
            if (authority.toString().equalsIgnoreCase("s5")) {
                b = spot_checkExceptionService.acceptWorkOrders(userEntity.getUid(),SCSailings,date,pointName);
            }
        }
        resultInfo.setFlag(b);



        packagedData.writeValue(resultInfo,httpServletResponse);
    }
    /**修改状态的确认*/
    @RequestMapping("reviseStatusDetermine")
    public void reviseStatusDetermine(@RequestParam Long date,@RequestParam String pointContent,@RequestParam String SCSailings,@RequestParam String SCStatusSelect,HttpServletResponse httpServletResponse,HttpServletRequest httpServletRequest) throws IOException {
        PackagedData packagedData = new PackagedDataImpl();
        ResultInfo resultInfo = new ResultInfo();
        boolean b = false;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();

        for (GrantedAuthority authority : authorities) {
            if (authority.toString().equalsIgnoreCase("s5")) {
                b = spot_checkExceptionService.reviseStatusDetermine(SCStatusSelect,SCSailings,date,pointContent);
            }
        }
        if (b){
            resultInfo.setFlag(true);
        }else {
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("状态修改失败!");
        }
        packagedData.writeValue(resultInfo,httpServletResponse);
    }

}
